package com.triones.yjj.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.triones.yjj.entity.ActivityDO;
import com.triones.yjj.entity.MemberBrowseDO;
import com.triones.yjj.entity.MemberBrowseRecordDO;
import com.triones.yjj.entity.MemberDistributionRecordDO;
import com.triones.yjj.entity.bo.DistributionParams;
import com.triones.yjj.entity.bo.MemberBrowseEndParams;
import com.triones.yjj.entity.bo.MemberBrowseStartParams;
import com.triones.yjj.entity.bo.WxVisitParams;
import com.triones.yjj.entity.vo.*;
import com.triones.yjj.mapper.*;
import com.triones.yjj.service.MemberBrowseService;
import com.triones.yjj.service.MemberDistributionRecordService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户分销记录 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class MemberDistributionRecordServiceImpl implements MemberDistributionRecordService {

    private final MemberDistributionRecordMapper memberDistributionRecordMapper;

    public TableDataInfo<WxMemberDistributionRecordVO> getList(Long memberId, PageQuery pageQuery) {
        QueryWrapper<MemberDistributionRecordDO> wrapper = new QueryWrapper();
        wrapper.eq("a.member_id",memberId);
        wrapper.orderByDesc(" a.create_time");
//        Page<MemberDistributionRecordVO> result = memberDistributionRecordMapper.selectVoPage(pageQuery.build(),wrapper);
        Page<WxMemberDistributionRecordVO> result = memberDistributionRecordMapper.getMemberDistributionPageList(pageQuery.build(),wrapper);
        return TableDataInfo.build(result);
    }

    public TableDataInfo<WxMemberDistributionRecordVO> getOneDistributionRecordList(Long memberId,Long activityId,  PageQuery pageQuery) {
        QueryWrapper<MemberDistributionRecordDO> wrapper = new QueryWrapper();
        wrapper.eq("a.distribution_grade",1);
        wrapper.eq("a.activity_id",activityId);
        wrapper.eq("a.member_id",memberId);
        wrapper.orderByDesc(" a.create_time");
        Page<WxMemberDistributionRecordVO> result = memberDistributionRecordMapper.getMemberOneDistributionPageList(pageQuery.build(),wrapper);
        return TableDataInfo.build(result);
    }

    public TableDataInfo<WxMemberDistributionRecordVO> getTwoDistributionRecordList(Long memberId,Long activityId, PageQuery pageQuery) {
        QueryWrapper<MemberDistributionRecordDO> wrapper = new QueryWrapper();
        wrapper.eq("a.distribution_grade",2);
        wrapper.eq("a.activity_id",activityId);
        wrapper.eq("a.member_id",memberId);
        wrapper.orderByDesc(" a.create_time");
        Page<WxMemberDistributionRecordVO> result = memberDistributionRecordMapper.getMemberTwoDistributionPageList(pageQuery.build(),wrapper);
        return TableDataInfo.build(result);
    }

    public Map<String,Object> getDistributionCount(Long memberId,Long activityId,Integer distributionGrade) {
        Map<String,Object> result = memberDistributionRecordMapper.getMemberDistributionCount(memberId,activityId,distributionGrade);
        return result;
    }

    @Override
    public Map distributionStats(Long activityId) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        Map map = new HashMap();
        BigDecimal distributionAmount = BigDecimal.ZERO;
        BigDecimal oneDistributionAmount = BigDecimal.ZERO;
        BigDecimal twoDistributionAmount = BigDecimal.ZERO;
        int oneDistributionNumber = 0;
        int twoDistributionNumber = 0;

        /**
         * 总的分销金额
         */
        LambdaQueryWrapper<MemberDistributionRecordDO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberDistributionRecordDO::getActivityId,activityId)
            .eq(MemberDistributionRecordDO::getUserId,loginUser.getUserId());
        List<MemberDistributionRecordDO> memberDistributionRecordDOS = memberDistributionRecordMapper.selectList(lqw);
        if (memberDistributionRecordDOS != null && memberDistributionRecordDOS.size() > 0) {
            for (MemberDistributionRecordDO memberDistributionRecordDO : memberDistributionRecordDOS) {
                BigDecimal kickback = memberDistributionRecordDO.getKickback();
                distributionAmount = distributionAmount.add(kickback);
                if (memberDistributionRecordDO.getDistributionGrade() == 1) {
                    oneDistributionAmount = oneDistributionAmount.add(kickback);
                    oneDistributionNumber += 1;
                } else if (memberDistributionRecordDO.getDistributionGrade() == 2) {
                    twoDistributionAmount = twoDistributionAmount.add(kickback);
                    twoDistributionNumber += 1;
                }
            }
        }
        map.put("distributionAmount",distributionAmount);
        map.put("oneDistributionAmount",oneDistributionAmount);
        map.put("twoDistributionAmount",twoDistributionAmount);
        map.put("oneDistributionNumber",oneDistributionNumber);
        map.put("twoDistributionNumber",twoDistributionNumber);
        return map;
    }

    @Override
    public TableDataInfo<DistributionVO> distributionPageList(DistributionParams params, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        params.setUserId(loginUser.getUserId());
        Page<DistributionVO> result = memberDistributionRecordMapper.distributionRecordPageList(pageQuery.build(),params);
        return TableDataInfo.build(result);
    }

}
